/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cdtserver;

/**
 *
 * @author Airton
 */
public class Message {
    String origen;
    String destiny;
    connectionType type; //   00 dados, 01 ack, 11 reject, 10 requestConnection
    byte sequence_offset; //num of sequence of packet while sending, in confirm or rejecting, 0-2^n -1
    byte[] data; //dados 50 bytes max
    byte[] crc; //checksum information 4 bytes
    static int overHeadOffSet = 22; //Pulando area de controle

    public Message(){}
    public Message(byte[] data, int size){
        origen = "";
        destiny = "";
        this.data = new byte[128];
        crc = new byte[16];
        
        //Setting origen
        int i = 0;
        for( i = 0; i<= 9; i++)
            origen.concat( String.valueOf( data[i] ));

        //Setting destiny
        for(i=10; i<= 19; i++  )
            destiny.concat(String.valueOf( data[i]));

        setType(data[20]);
        setSequence_offset(data[21]);
        setData(data);
    }

    public Message(byte[] ldata, byte[] lcrc, String lorigen, String ldestiny, byte ltype, byte seq){
        data = ldata;
        crc = lcrc;
        origen = lorigen;
        destiny = ldestiny;
        setType(ltype);
        sequence_offset = seq;
    }

    public byte[] getCrc() {
        return crc;
    }

    public void setCrc(byte[] crc) {
        this.crc = crc;
    }

    public byte[] getData() {
        return data;
    }

    public boolean checkCrc(byte[] crc){
            return true;
    }

    public void setData(byte[] data){
        //Escapando overhead
        //Preenchendo area de dados com 50 bytes, iniciando pelo byte area de dados
        for (int i=overHeadOffSet; i<= overHeadOffSet+50; i++)
            this.data[i-overHeadOffSet] += data[i];
    }

    public String getDestiny() {
        return destiny;
    }

    public void setDestiny(String destiny) {
        this.destiny = destiny;
    }

    public String getOrigen() {
        return origen;
    }

    public void setOrigen(String origen) {
        this.origen = origen;
    }

    public byte getSequence_offset() {
        return sequence_offset;
    }

    public void setSequence_offset(byte sequence_offset) {
        this.sequence_offset = sequence_offset;
    }

    public connectionType getType() {
        connectionType connType=connectionType.requestConnection;
        return connType;
    }

    public void setType(byte type) {
        Integer valueType = Integer.valueOf(type);
        switch(valueType ){
            case 0:
                this.type = connectionType.dataPacket;
                break;
            case 1:
                this.type = connectionType.Ack;
                break;
            case 3:
                this.type = connectionType.reject;
                break;
            case 4:
                this.type = connectionType.requestConnection;
        }
    }
}
